﻿using AutoMapper;
using Easy.Common.Params;
using Easy.Common.Results;
using Easy.Services.Dtos;
using Easy.Services.Entities;
using Easy.Services.Interface;
using Microsoft.AspNetCore.Mvc;

namespace Easy.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController(IMapper _mapper, IRoleService _roleService, IRolePermissionRelationService _relationService) : BaseController
    {
        readonly IMapper mapper = _mapper;

        readonly IRoleService roleService = _roleService;

        readonly IRolePermissionRelationService relationService = _relationService;


        [HttpPost]
        public APIResult Add(RoleDTO dto)
        {
            APIResult result = new();
            dto.Name = dto.Name.Trim();
            var isExist = roleService.Exist(r => r.Name.Equals(dto.Name) && r.IsDelete == false);
            if (isExist)
            {
                result.Message = "角色已存在";
                return result;
            }
            Role role = mapper.Map<Role>(dto);
            role.SetModelBase(tokenModel: TokenModel, true);
            var id = roleService.Add(role);
            if (id > 0)
            {
                result.Success = true;
                result.Message = "添加成功";
            }
            else
            {
                result.Message = "添加失败";
            }
            return result;

        }


        [HttpPost]
        public APIResult Update(RoleDTO dto)
        {
            Role role = mapper.Map<Role>(dto);
            role.SetModelBase(tokenModel: TokenModel);
            var result = roleService.Update(role);
            return result;

        }

        [HttpDelete]
        public APIResult Delete(long id)
        {
            if (id <= 0)
            {
                return new APIResult() { Message = "异常数据" };
            }
            if (id == 1)
            {
                return new APIResult() { Message = "系统保留不允许删除" };
            }
            var result = roleService.Delete(id: id, tokenModel: TokenModel);
            return result;

        }


        [HttpGet]
        public APIResult Get(long id)
        {
            APIResult result = new()
            {
                Success = true,
                Data = roleService.Get(r => r.Id == id)
            };
            return result;

        }

        [HttpGet]
        public APIResult GetDTO(long id)
        {
            APIResult result = new()
            {
                Success = true,
                Data = roleService.GetDTO(id)
            };
            return result;

        }

        [HttpGet]
        public APIResult GetList()
        {
            APIResult result = new()
            {
                Success = true,
                Data = roleService.GetList().Select(x => new { x.Id, x.Name })
            };
            return result;

        }

        [HttpGet]
        public APIResult GetEnableList()
        {
            APIResult result = new()
            {
                Success = true,
                Data = roleService.GetList(r => r.State == 1).Select(x => new { x.Id, x.Name })
            };
            return result;

        }


        [HttpGet]
        public APIResult GetPage(int page, int pageSize) => roleService.GetPage(new PageParam() { Page = page, PageSize = pageSize });


        [HttpPost]
        public APIResult AssignPermission(RolePermissionRelationDTO dto)
        {
            APIResult result = new();
            List<RolePermissionRelation> relations = [];
            foreach (var item in dto.PermissionIds)
            {
                var model = new RolePermissionRelation() { RoleId = dto.RoleId, PermissionId = item };
                model.SetModelBase(tokenModel: TokenModel);
                relations.Add(model);
            }
            if (relations.Count <= 0)
            {
                result.Message = "参数为空";
                return result;
            }
            var list = relationService.GetList(r => r.RoleId == dto.RoleId);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    item.SetModelBase(tokenModel: TokenModel);
                    item.SetDelete();
                }
                relationService.UpdateBatch(list);
            }
            result.Success = relationService.AddBatch(relations);
            result.Message = result.Success ? "添加成功" : "添加失败";
            return result;
        }


    }
}
